/*
 * 
 * Libman
 * 
 * Daniel Kaufmann, Martin Lang, Simon Moosbrugger,
 * Stefan Mayer, Stephan Halbeisen
 * 
 */
package model.dao;

import hibernate.HibernateUtil;

import java.util.List;

import model.Exemplar;
import model.Loan;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import application.exceptions.ExemplarNotFoundException;

/**
 * A DAO implementation for Loan objects.
 */

public class LoanDAO extends GenericHibernateDAO<Loan, Long> {
	// implementation in parent class
	/**
	 * 
	 * Gets the open loans.
	 * 
	 * @param exemplarID
	 *            the exemplar id
	 * @param maxResults
	 *            the max results
	 * @return the open loans
	 * @throws ExemplarNotFoundException
	 *             the exemplar not found exception
	 */
	public List<Loan> getOpenLoans(int exemplarID, int maxResults)
			throws ExemplarNotFoundException {
		Exemplar ex = DAOFactory.getInstance().getExemplarDAO()
				.findById(exemplarID, false);

		if (ex == null) {
			throw new ExemplarNotFoundException();
		}

		Session session = HibernateUtil.currentSession();
		Criteria crit = session.createCriteria(Loan.class);

		crit.add(Restrictions.eq("exemplar", ex));
		crit.add(Restrictions.eq("state", 1));

		@SuppressWarnings("unchecked")
		List<Loan> loanList = crit.list();

		return loanList;
	}
}